<?php 
header("Content-Type:text/html;charset=utf-8");
session_start();
include "includes/init.php";
error_reporting(E_ERROR | E_PARSE);
global $result;
$time = time();
$phone=isset($_SESSION['phone'])?$_SESSION['phone']:'';
$mid=isset($_SESSION['id'])?$_SESSION['id']:'';
    if (isset($_GET['usertype']) == 1) {
        // 供应商
        $sql = "SELECT DISTINCT
                m.company_name,
                m.company_data
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mifeng = 0
                    AND p.type = b.btype
                    AND b.mid = m.id
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < '".$time."'
                AND p.type = b.btype
                AND b.mid = m.id
            )";
        $company_name = $db_Obj->getall($sql);
        // 项目
        $sql = "SELECT DISTINCT
                p.project
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.type = b.btype
                    AND p.mifeng = 0
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < ".$time."
                AND p.type = b.btype
            )
            ORDER BY
                p.type ASC,
                b.price ASC ";
        $project = $db_Obj ->getall($sql);
    }else{
          // 供应商
        $sql = "SELECT DISTINCT
                m.company_name,
                m.company_data
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mifeng = 0
                    AND p.nphone = '".$phone."'
                    AND p.type = b.btype
                    AND b.mid = m.id
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < '".$time."'
                AND p.nphone = '".$phone."'
                AND p.type = b.btype
                AND b.mid = m.id
            )";
        $company_name = $db_Obj->getall($sql);
        // 项目
        $sql = "SELECT DISTINCT
                p.project
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mid = '".$mid."'
                    AND p.type = b.btype
                    AND p.mifeng = 0
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < ".$time."
                AND p.mid = '".$mid."'
                AND p.type = b.btype
            )
            ORDER BY
                p.type ASC,
                b.price ASC ";
        $project = $db_Obj ->getall($sql);
    }

    //产品信息---->公司
    if(!empty($_POST['company_name'])){
        $company = $_POST['company_name'];
            $sql = "
            SELECT
            m.company_name,
            m.company_data,
            p.nphone,
            p.lasttime,
            p.units,
            p.currency,
            p.number,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
            p.price_type,
            p.validtime,
            p.invalidtime,
            b.price,
            b.btype,
            b.bsize,
            b.delivery,
            b.MOQ,
            b.MPQ,
            b.createtime,
            b.rate
        FROM
            t_product AS p,
            t_bid AS b,
            t_member AS m
        WHERE
            (
                p.mifeng = 0
                AND p.type = b.btype
                AND b.mid = m.id
                AND m.company_name = '".$company."'
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < '".$time."'
            AND p.type = b.btype
            AND b.mid = m.id
            AND m.company_name = '".$company."'
        );
            ";
        $result = $db_Obj->getall($sql);
    }

    // 公司数据
    if(!empty($_POST['company_name'])){
    	$company_names = $_POST['company_name'];
    	$csql = "select * from t_member where company_name = '".$company_names."' ";
    	$query = $db_Obj->getall($csql);
    }
    //产品数据
    if(!empty($_POST['project'])){
    	$pro = $_POST['project'];
            $sql = "SELECT
            m.company_name,
            m.company_data,
            p.nphone,
            p.project,
            p.lasttime,
            p.units,
            p.currency,
            p.number,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
            p.validtime,
            p.invalidtime,
            b.price,
            b.btype,
            b.bsize,
            b.delivery,
            b.MOQ,
            b.MPQ,
            b.createtime,
            b.rate
        FROM
            t_member AS m,
            t_product AS p,
            t_bid AS b
        WHERE
            (
                p.mifeng = 0 
                AND p.project = '".$pro."'
                AND b.mid = m.id
                AND p.type = b.btype
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < ".$time."
            AND p.project = '".$pro."'
            AND b.mid = m.id
            AND p.type = b.btype
        )   
        ORDER BY p.type asc, b.price asc
        ";
    	$result = $db_Obj->getall($sql);
    }



// 导出Excel
if(!empty($_POST['export'])){
    $export = $_POST['export'];
    if(!empty($_POST['project'])){
        $pro = $_POST['project'];
         $sql = "
            SELECT
            m.company_name,
            m.company_data,
            p.nphone,
            p.project,
            p.lasttime,
            p.units,
            p.currency,
            p.number,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
            p.price_type,
            b.price,
            b.bnumber,
            b.btype,
            b.bsize,
            b.delivery,
            b.MOQ,
            b.MPQ,
            b.createtime,
            b.rate
        FROM
            t_member AS m,
            t_product AS p,
            t_bid AS b
        WHERE
            (
                p.mifeng = 0 
                AND p.project = '".$pro."'
                AND b.mid = m.id
                AND p.type = b.btype
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < ".$time."
            AND p.project = '".$pro."'
            AND b.mid = m.id
            AND p.type = b.btype
        )   
        ORDER BY p.type asc, b.price asc
        ";
            $result = $db_Obj->getall($sql);
        }
        if(!empty($_POST['company_name'])){
            $company = $_POST['company_name'];

            $sql = "
                SELECT
                m.company_name,
                m.company_data,
                p.nphone,
                p.lasttime,
                p.project,
                p.units,
                p.currency,
                p.number,
                p.price_type,
                b.price,
                b.btype,
                b.bsize,
                b.bnumber,
                b.delivery,
                b.MOQ,
                b.MPQ,
                b.createtime,
                b.rate
            FROM
                t_product AS p,
                t_bid AS b,
                t_member AS m
            WHERE
                (
	                p.mifeng = 0
	                AND p.type = b.btype
	                AND b.mid = m.id
	                AND m.company_name = '".$company."'
	            )
	        OR (
	            p.mifeng = 1
	            AND p.lasttime < '".$time."'
	            AND p.type = b.btype
	            AND b.mid = m.id
	            AND m.company_name = '".$company."'
	        );
	            ";
            $result = $db_Obj->getall($sql);
        }

    }

    if ($export == 1) {
        $columns = array(
            array(
                'title' => '序号',
                'field' => 'xuhao',
                'width' => 20
            ),
            array(
                'title' => '项目',
                'field' => 'project',
                'width' => 20
            ),
            array(
                'title' => '物料编码',
                'field' => 'btype',
                'width' => 20
            ),
            array(
                'title' => '规格',
                'field' => 'bsize',
                'width' => 20
            ),
            array(
                'title' => '供应商',
                'field' => 'company_name',
                'width' => 20
            ),
            array(
                'title' => '供应商代码',
                'field' => 'company_data',
                'width' => 20
            ),
            array(
                'title' => '数量',
                'field' => 'bnumber',
                'width' => 20
            ),
            array(
                'title' => '计量单位',
                'field' => 'units',
                'width' => 20
            ),
            array(
                'title' => '未税价位',
                'field' => 'price',
                'width' => 20
            ),
            array(
                'title' => '税率(%)',
                'field' => 'rate',
                'width' => 20
            ),
            array(
                'title' => '币种',
                'field' => 'currency',
                'width' => 20
            ),
            array(
                'title' => '交期(天)',
                'field' => 'delivery',
                'width' => 20
            ),
            array(
                'title' => 'MOQ(最小订单)',
                'field' => 'MOQ',
                'width' => 20
            ),
            array(
                'title' => 'MPQ(最小包装)',
                'field' => 'MPQ',
                'width' => 20
            ),
            array(
                'title' => '询价类型',
                'field' => 'price_type',
                'width' => 20
            ),
                
            array(
                'title' => '报价截止日期',
                'field' => 'lasttime',
                'width' => 20
            ),
            array(
                'title' => '报价日期',
                'field' => 'createtime',
                'width' =>20
            ),
            array(
                'title' => '物料组',
                'field' => 'material',
                'width' => 20
            ),
            array(
                'title' => '采购组',
                'field' => 'purchase',
                'width' => 20
            ),
            array(
                'title' => '工厂组',
                'field' => 'plant',
                'width' => 20
            ),
            array(
                'title' => '额数',
                'field' => 'extranum',
                'width' => 20
            )
        );

       foreach ($result as $key => $value) {
            $exportlist['xuhao'] =$key+1;
            $exportlist['project'] =$value['project'];
            $exportlist['btype'] =$value['btype'];
            $exportlist['bsize'] =$value['bsize'];
            $exportlist['company_name'] =$value['company_name'];
            $exportlist['company_data'] =$value['company_data'];
            $exportlist['bnumber'] =$value['bnumber'];
            $exportlist['units'] =$value['units'];
            $exportlist['price'] =$value['price'];
            $exportlist['rate'] =$value['rate'];
            $exportlist['currency'] =$value['currency'];
            $exportlist['delivery'] =$value['delivery'];
            $exportlist['MOQ'] =$value['MOQ'];
            $exportlist['MPQ'] =$value['MPQ'];
            $exportlist['price_type'] =$value['price_type'];
            $exportlist['lasttime'] = date('Y-m-d H:i:s',$value['lasttime']);
            $exportlist['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            $exportlist['material'] =$value['material'];
            $exportlist['purchase'] =$value['purchase'];
            $exportlist['plant'] =$value['plant'];
            $exportlist['extranum'] =$value['extranum'];
            $arr[] = $exportlist;
        }
       $excObj->export($arr, array(
            "title" => "数据-" . date('Y-m-d-H-i', time()),
            "columns" => $columns
        ));
    }

include 'table3.html';
